/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2015-2-14         yellowlgx
 *
 */

package com.skyworth.ui.menu;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skyworth.commen.ui.R;
import com.skyworth.util.MyFocusFrame;
import com.skyworth.util.SkyScreenParams;

/**
 * @ClassName SkyMenuItem
 * @Description TODO (write something)
 * @author yellowlgx
 * @date 2015-2-14
 * @version TODO (write something)
 */
public class SkyMenuItem_2 extends FrameLayout implements OnFocusChangeListener
{
    private Context mContext;
    private LinearLayout contentLayout;

    private ImageView itemIconView, itemCornerIconView;
    private int focusIconID = -1, unFocusIconID = -1;

    private TextView itemTextView; // 显示内容
    private int textSize = 0;

    private SkyMenuData currentData;

    private boolean isSelect = false;

    private MyFocusFrame menuFocusView;
    private String colorString = "5b5b5b";

    public SkyMenuItem_2(Context context)
    {
        super(context);
        mContext = context;
        initView();
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        this.setOnFocusChangeListener(this);
    }

    // 初始化控件
    private void initView()
    {
        focusIconID = R.drawable.ui_sdk_second_focus;
        unFocusIconID = R.drawable.ui_sdk_second_focus;

        contentLayout = new LinearLayout(mContext);
        // contentLayout.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(52),
        // SkyScreenParams.getInstence(mContext).getResolutionValue(0), SkyScreenParams
        // .getInstence(mContext).getResolutionValue(45), 0);
        contentLayout.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(52), 0, 0,
                0);
        contentLayout.setOrientation(LinearLayout.HORIZONTAL);
        contentLayout.setGravity(Gravity.CENTER_VERTICAL);

        itemIconView = new ImageView(mContext);
        LinearLayout.LayoutParams img_p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        img_p.rightMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(15);
        contentLayout.addView(itemIconView, img_p);
        itemTextView = new TextView(mContext);
        itemTextView.setSingleLine(true);
        itemTextView.getPaint().setAntiAlias(true);
        itemTextView.setSingleLine();
        itemTextView.setEllipsize(TruncateAt.END);
        itemTextView.setMarqueeRepeatLimit(-1);
        // itemTextView.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(4), 0,
        // 0,
        // 0);
        itemTextView.setGravity(Gravity.CENTER_VERTICAL);
        LinearLayout.LayoutParams title_p = new LinearLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(200),
                LayoutParams.MATCH_PARENT);
        title_p.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(4);
        contentLayout.addView(itemTextView, title_p);

        itemCornerIconView = new ImageView(mContext);
        itemCornerIconView.setVisibility(View.GONE);
        LinearLayout.LayoutParams corner_p = new LinearLayout.LayoutParams(
                SkyScreenParams.getInstence(mContext).getResolutionValue(38),
                SkyScreenParams.getInstence(mContext).getResolutionValue(90));
        contentLayout.addView(itemCornerIconView, corner_p);
        this.addView(contentLayout,
                new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    private int getTextViewStringWidth(String title)
    {
        Paint paint = new Paint();
        paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize,
                getResources().getDisplayMetrics()));
        return (int) paint.measureText(title);
    }

    private void setCornerIconPosition(SkyMenuData data)
    {
        if (itemCornerIconView != null && !TextUtils.isEmpty(data.getItemTitle()))
        {
            if (data.getItemCornerIcon() != -1)
            {
                itemCornerIconView.setBackgroundResource(data.getItemCornerIcon());
                if (itemCornerIconView.getVisibility() != View.VISIBLE)
                    itemCornerIconView.setVisibility(View.VISIBLE);
            }
            LinearLayout.LayoutParams cornerP = (LinearLayout.LayoutParams) itemCornerIconView
                    .getLayoutParams();
            int titleWidth = getTextViewStringWidth(data.getItemTitle());
            if (titleWidth > SkyScreenParams.getInstence(mContext).getResolutionValue(200))
            {
                cornerP.leftMargin = SkyScreenParams.getInstence(mContext).getResolutionValue(10);
            } else
            {
                if (data.getItemState())
                {
                    cornerP.leftMargin = (titleWidth
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(10)
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(16))
                            - SkyScreenParams.getInstence(mContext).getResolutionValue(200);
                } else
                {
                    cornerP.leftMargin = (titleWidth
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(10)
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(4))
                            - SkyScreenParams.getInstence(mContext).getResolutionValue(200);
                }
            }
            itemCornerIconView.setLayoutParams(cornerP);
        }
    }

    public void setFocusView(MyFocusFrame menuFocusView)
    {
        this.menuFocusView = menuFocusView;
    }

    /**
     * 设置文字内容属性
     * 
     * @param size
     *            大小
     * @param color
     *            颜色
     * @date 2015-2-14
     */
    public void setTextAttribute(int size, int color)
    {
        if (itemTextView != null)
        {
            textSize = size;
            itemTextView.setTextSize(size);
            itemTextView.setTextColor(getResources().getColor(color));
        }
    }

    public void setSelectState(boolean select)
    {
        isSelect = select;
        if (isSelect)
            itemIconView.setVisibility(View.VISIBLE);
        else if (!isSelect)
            itemIconView.setVisibility(View.INVISIBLE);
    }

    public void refreshItemValue(SkyMenuData data)
    {
        if (data != null)
        {
            currentData = data;
            if (data.getItemTitle() != null)
                itemTextView.setText(data.getItemTitle());//+ "　");
            setCornerIconPosition(data);
            this.post(new Runnable()
            {
                @Override
                public void run()
                {
                    int x = SkySecondMenu.preViewWidth
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(50)
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(30);
                    int y = SkyScreenParams.getInstence(mContext).getResolutionValue(516);
                    int width = SkyMenuItem_2.this.getWidth()
                            + SkyScreenParams.getInstence(mContext).getResolutionValue(47) * 2;
                    int height = SkyScreenParams.getInstence(mContext).getResolutionValue(170);

                    // menuFocusView.changeFocusPos(SkyMenuItem_2.this);
                }
            });
        }
    }

    public void updataItemValue(SkyMenuData data)
    {
        if (data != null)
        {
            currentData = data;
            if (data.getItemTitle() != null)
                itemTextView.setText(data.getItemTitle());// + "　");
            setCornerIconPosition(data);
            itemTextView.setTextColor(getResources().getColor(R.color.c_4));

            if (data.getItemState())
            {
                itemIconView.setVisibility(View.INVISIBLE);
                itemTextView.setPadding(
                        SkyScreenParams.getInstence(mContext).getResolutionValue(16), 0, 0, 0);
            } else
            {
                itemIconView.setBackgroundResource(unFocusIconID);
                itemTextView.setPadding(SkyScreenParams.getInstence(mContext).getResolutionValue(4),
                        0, 0, 0);
            }
        }
    }

    public SkyMenuData getItemData()
    {
        return currentData;
    }

    public void reSetItemState()
    {
        if (unFocusIconID != -1 && !currentData.getItemState())
            itemIconView.setBackgroundResource(unFocusIconID);
    }

    public void focus(View v)
    {
        if (focusIconID != -1 && !currentData.getItemState())
            itemIconView.setBackgroundResource(focusIconID);
        itemTextView.setTextColor(getResources().getColor(R.color.a_0));

        int[] location = new int[2];
        v.getLocationInWindow(location);
        int shaderSize = SkyScreenParams.getInstence(mContext).getResolutionValue(83);
        int x = location[0] - shaderSize;
        int y = location[1] - shaderSize;
        int width = SkyScreenParams.getInstence(mContext).getResolutionValue(324) + shaderSize * 2;
        int height = SkyScreenParams.getInstence(mContext).getResolutionValue(90) + shaderSize * 2;

        // menuFocusView.changeFocusPos(x, y, width, height);
    }

    public void unfocus()
    {
        if (isSelect && unFocusIconID != -1 && !currentData.getItemState())
            itemIconView.setBackgroundResource(unFocusIconID);
        itemTextView.setTextColor(getResources().getColor(R.color.c_4));
    }
    
    public void refreshOnThemeChanged()
    {
        if (this.hasFocus())
        {
            itemTextView.setTextColor(getResources().getColor(R.color.a_0));
        } else
        {
            itemTextView.setTextColor(getResources().getColor(R.color.c_4));
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus)
    {
        if (hasFocus)
        {
//            Log.v("xw", " itemTextView  " + itemTextView.getText().toString());
            itemTextView.setEllipsize(TruncateAt.MARQUEE);
            itemTextView.setSelected(true);
            focus(v);
        } else
        {
            unfocus();
            itemTextView.setSelected(false);
            itemTextView.setEllipsize(TruncateAt.END);
        }
    }
}
